cmake_minimum_required(VERSION 3.16)

project(seqiot C)
set(CMAKE_C_STANDARD 99)

if (DEFINED ENV{CROSS_TARGET})
    set(CMAKE_SYSTEM_NAME Linux)
    set(CROSS_TARGET $ENV{CROSS_TARGET})
    set(TOOLCHAIN /opt/toolchain/${CROSS_TARGET}/usr)
    set(CMAKE_C_COMPILER ${TOOLCHAIN}/bin/arm-linux-gcc)
    set(CMAKE_CXX_COMPILER /${TOOLCHAIN}/bin/arm-linux-g++)
    set(CMAKE_AR /${TOOLCHAIN}/bin/arm-linux-ar)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/dists/${CROSS_TARGET}/home)

    include_directories(/${TOOLCHAIN}/local/include)
    link_directories(/${TOOLCHAIN}/local/lib)
else ()
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/dists/home)
endif ()

set(CMAKE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

aux_source_directory(src/mbedtls src_mbedtls)
add_library(mbedtls STATIC ${src_mbedtls})

aux_source_directory(src/linksdk src_linksdk)
add_library(linksdk STATIC ${src_linksdk})
target_link_libraries(linksdk mbedtls)

aux_source_directory(src/common src_common)
aux_source_directory(src/model src_model)
add_library(seqiot STATIC ${src_common} ${src_model})
target_link_libraries(seqiot m pthread sqlite3)

include_directories(src/mbedtls src/linksdk src/common src/model)
link_libraries(seqiot log4c)

add_executable(ntpcli src/utils/main_ntpcli.c)

add_executable(atcmd src/utils/main_atcmd.c)
target_link_libraries(atcmd 60870)

add_executable(ltepwr src/utils/main_ltepwr.c)

add_executable(ppping src/utils/main_ppping.c)

add_executable(modump src/utils/main_modump.c src/common/gpio.c)
target_link_libraries(modump modbus)

add_executable(bridge src/utils/main_bridge.c)

add_executable(tsdb src/utils/main_tsdb.c)
target_link_libraries(tsdb sqlite3)

aux_source_directory(src/daemon src_daemon)
add_executable(daemon ${src_daemon})
target_link_libraries(daemon sqlite3)

aux_source_directory(src/dbms src_dbms)
add_executable(dbms ${src_dbms})
target_link_libraries(dbms sqlite3 cjson)

add_executable(sysinfo src/utils/main_sysinfo.c)
target_link_libraries(sysinfo seqiot)

add_executable(model src/utils/main_model.c)
target_link_libraries(model seqiot)

aux_source_directory(src/modbus src_modbus)
add_executable(modbus_acquire ${src_modbus})
target_link_libraries(modbus_acquire modbus sqlite3)

aux_source_directory(src/dlt645 src_dlt645)
add_executable(dlt645_acquire ${src_dlt645})
target_link_libraries(dlt645_acquire sqlite3)

#
#add_executable(iec101_acquire iec104/main_iec101.c
#        iec104/apdu.c
#        iec104/sched_iec60870.c
#        iec104/iec101.c
#        iec104/master_iec101.c)
#target_link_libraries(iec101_acquire seqiot)

aux_source_directory(src/iec104 src_iec104)
add_executable(iec104_pub ${src_iec104})
target_link_libraries(iec104_pub 60870)

#add_executable(query tsdb/main_query.c)
#target_link_libraries(query seqiot)

aux_source_directory(src/hisdata src_hisdata)
add_executable(hisdata ${src_hisdata})
target_link_libraries(hisdata seqiot)

#add_executable(mqtt_ali
#        iec104/iec101.c
#        mqtt/scada.c
#        mqtt/packet.c
#        mqtt/sched_mqtt.c
#        mqtt/watchdog.c)
#target_link_libraries(mqtt_ali seqiot)

aux_source_directory(src/mqtt src_mqtt)
aux_source_directory(src/iec60870 src_iec60870)
add_executable(mqtt_pub ${src_mqtt} ${src_iec60870})
target_link_libraries(mqtt_pub sqlite3 mosquitto linksdk)

add_executable(mqtt_bash src/utils/main_mqtt_bash.c)
target_link_libraries(mqtt_bash mosquitto)

aux_source_directory(src/aliot src_aliot)
add_executable(mqtt_aliot ${src_aliot} ${src_iec60870} src/model/sysinfo.c src/mqtt/scada.c src/mqtt/mqtt.c src/mqtt/sched_mqtt.c)
target_link_libraries(mqtt_aliot pthread sqlite3 mbedtls linksdk seqiot mosquitto modbus)

add_library(testlib src/dbms/param.c src/dbms/param.h)
add_executable(test src/utils/main_test.c)
target_link_libraries(test testlib cjson pthread sqlite3 mbedtls linksdk seqiot mosquitto modbus)


add_library(vqc SHARED src/jni/library.c src/jni/cJSON.c src/jni/vqc.c)

add_executable(hisdump src/utils/main_hisdump.c)
